import datetime
import calendar
class WorkDurationCalculator:
    def __init__(self, year):
        self.year = year
        self.holidays = self.get_holidays()

    def get_holidays(self):
        holidays = []
        for month in range(1, 13):
            first_day = datetime.date(self.year, month, 1)
            last_day = datetime.date(self.year, month, calendar.monthrange(self.year, month)[1])
            for day in range(first_day.day, last_day.day + 1):
                date = datetime.date(self.year, month, day)
                if calendar.isleap(self.year) and date == datetime.date(self.year, 2, 29):
                    continue
                elif date.weekday() >= 5:
                    holidays.append(date)
        return holidays

    def is_workday(self, date):
        return date.weekday() < 5 and date not in self.holidays

    def calculate_work_duration(self, start_time, end_time):
        work_start = datetime.time(8, 30)
        work_end = datetime.time(17, 30)
        start_datetime = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
        end_datetime = datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S')
        work_duration = datetime.timedelta()
        current_date = start_datetime.date()
        while current_date <= end_datetime.date():
            if self.is_workday(current_date):
                work_start_datetime = datetime.datetime.combine(current_date, work_start)
                work_end_datetime = datetime.datetime.combine(current_date, work_end)
                if start_datetime <= work_start_datetime and end_datetime < work_start_datetime and start_datetime < end_datetime:
                    start_datetime = start_datetime
                    end_datetime = end_datetime
                if start_datetime <= work_start_datetime and end_datetime < work_start_datetime and start_datetime > end_datetime:
                    work_duration = datetime.timedelta(hours=0, minutes=5, seconds=0)
                    break
                if start_datetime < work_start_datetime and work_start_datetime < end_datetime < work_end_datetime:
                    start_datetime = work_start_datetime
                    end_datetime = end_datetime
                if work_end_datetime > start_datetime > work_start_datetime and work_start_datetime < end_datetime < work_end_datetime:
                    start_datetime = start_datetime
                    end_datetime = end_datetime
                if work_end_datetime > start_datetime > work_start_datetime and end_datetime > work_end_datetime:
                    end_datetime = work_end_datetime
                    start_datetime = start_datetime
                if start_datetime > work_end_datetime and end_datetime > work_end_datetime:
                    start_datetime = start_datetime
                    end_datetime = end_datetime
                if start_datetime < work_start_datetime and end_datetime > work_end_datetime:
                    end_datetime = work_end_datetime
                    start_datetime = work_start_datetime
                work_duration += end_datetime - start_datetime
                break
            if not self.is_workday(current_date) and not self.is_workday(end_datetime.date()):
                work_duration = end_datetime - start_datetime
                break
            else:
                current_date += datetime.timedelta(days=1)
                start_datetime = datetime.datetime.combine(current_date, work_start)
        return work_duration.total_seconds()

instance = WorkDurationCalculator(2023)
seconds = instance.calculate_work_duration('2023-12-02 10:30:02','2023-12-02 11:20:24')
print(seconds)
date = datetime.datetime.strptime('2022-01-03 12:00:00', '%Y-%m-%d %H:%M:%S')
work_time = WorkDurationCalculator(2023)
is_workday = work_time.is_workday(date)
print(f"{date} is a workday: {is_workday}")

work_time = WorkDurationCalculator(2023)
year_holidays = work_time.get_holidays()
print(year_holidays)